<?php
namespace Admin\Controller;
use Think\Controller;
class MoviesController extends Controller {
    public function movieslist(){
    	//调用模型层获取数据
        $moviesModel=M('movies');
       // $type=M('type');
        // $this->type=$type->order("tid")->select();
        //var_dump($usermodel);
        $movies=$moviesModel->table('vm_movies m,vm_type t')->where('m.t_id=t.t_id')->select();
    	//调用视图层显示数据
    	$this->movies=$movies;

    	$this->display();
    }
    

 public function addmovies(){
        $typeModel=M('type');
        $this->type=$typeModel->order("t_id")->select();
        $this->display();
    }

    public function doaddmovies(){
       $movies = D("movies");
       
      

       if($movies->create()){
          if($_FILES['m_pic']['error']==0){
                $logoinfo = $this->addupload();
        $m_pic = $logoinfo['m_pic']['savename'];
      }else{
        $m_pic = "nopic.jpg";
      }

      $movies->m_pic = $m_pic;
     // $movies->uid=$_SESSION['alias'];
     // $movies->date=date("Y-m-d");
    
      if(false !== $movies->add()){
        $this->success('发表成功！',U('movieslist'));
      }else{
        $this->error('发表失败');
      }
      
    }else{
      $this->error($movies->getError());
    }
    }
     public function addupload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Admin/vmphoto/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                return $info;
                         }
    }


        public function detail($m_id){
        $moviesModel=M('movies');
         $typeModel=M('type');
     $movies=$moviesModel->table('vm_movies m,vm_type t')->where("m.m_id = $m_id and m.t_id=t.t_id")->select(); 
        $this->movies=$movies;
       $this->display();
    } 
    public function editmovies(){
        $m_id=$_GET['m_id'];
        $moviesModel=M('movies');//表明,conf中定义前缀
        $typeModel=M('type');

        $this->type=$typeModel->order("t_id")->select();

        $movies = $moviesModel->table('vm_movies m,vm_type t')->where("m.m_id=$m_id and m.t_id=t.t_id")->select();
        $this->movies=$movies;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }
    
    public function updatemovies(){

          $data=$_POST;
            $file=$_FILES;
            
            $m_id=$data['m_id'];
                   
            $moviesModel = M('movies'); // 实例化User模型
            // 要修改的数据对象属性赋值
         //  $result=$activityModel->where("act_id=$act_id")->save($data);
        
          if ($file['m_pic']['size']==0) {
            //没有修改头像
            $result=$moviesModel->where("m_id=$m_id")->save($data);
            
         
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Admin/vmphoto/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                


                $data['m_pic']= $info['m_pic']['savename']; 
                $result=$moviesModel->where("m_id=$m_id")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Admin/vmphoto/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
           
           if($result){
                $this->success('修改成功！','../Movies/movieslist');
                }
               else{
                   $this->error('修改失败！');
                  //$this->success('修改成功！','../Activity/activitylist');
                   }

    }



    public function delmovies($m_id){
   
    $moviesModel = M("movies"); // 实例化User对象
    $movies=$moviesModel->where("m_id=$m_id")->select();
    $result=$moviesModel->where("m_id=$m_id")->delete(); 
        if($result){                    
            $this->success('删除成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
            } else {
             $this->error('删除失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
            }

    }















    }